package com.xungou.order.controller;

import com.xungou.city.dao.ICity;
import com.xungou.good.service.IGoodService;
import com.xungou.order.pojo.Order;
import com.xungou.order.service.IOrderService;
import com.xungou.receiver.pojo.Receiver;
import com.xungou.receiver.service.IReceiverService;
import com.xungou.resource.service.IResourceService;
import com.xungou.sender.pojo.Sender;
import com.xungou.sender.service.ISenderService;
import com.xungou.shop.pojo.Shop;
import com.xungou.shop.service.IShopService;
import com.xungou.shoppingCart.service.IShoppingCartService;
import com.xungou.utils.FileUtil;
import com.xungou.utils.UserUtil;
import org.springframework.format.datetime.DateFormatter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/order")
public class OrderController {
    @Resource
    private IOrderService orderService;
    @Resource
    private IReceiverService receiverService;
    @Resource
    private ISenderService senderService;
    @Resource
    private ICity icity;
    @Resource
    private IShopService shopService;
    @Resource
    private IGoodService goodService;
    @Resource
    private IResourceService iResourceService;

    @RequestMapping("/add")
    @ResponseBody
    public Map<String, Object> add(@RequestParam Map<String, String> params) {
        Map<String, Object> result;
        try {
            result = orderService.addOrder(params);
        }catch (Exception e){
            result = new HashMap<String,Object>();
            result.put("error",e.getMessage());
        }
        return result;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Map<String, Object> delete(Order order) {
        int flag = orderService.deleteByPrimaryKey(order.getId());
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("flag",flag);
        return result;
    }

    @RequestMapping("/confirmOrderPage")
    public ModelAndView confirmOrderPage(){
        ModelAndView modelAndView = new ModelAndView();
        Map<String, Object> params = new HashMap<String,Object>();
        List<Receiver> receiverList = receiverService.list(params);
        modelAndView.addObject("receiverList",receiverList);
        modelAndView.setViewName("home/order/confirmOrder");
        return modelAndView;
    }

    @RequestMapping("/listForBuyer")
    @ResponseBody
    public List<Map<String,Object>> listForBuyer(@RequestParam Map<String, Object> params){
        params.put("userId",UserUtil.getCurrentUser().getId());
        List<Map<String,Object>> result = orderService.list(params);
        return result;
    }

    @RequestMapping("/listForShop")
    @ResponseBody
    public List<Map<String,Object>> listForShop(@RequestParam Map<String, Object> params){
        params.put("shopId",shopService.getShopByUserId(UserUtil.getCurrentUser().getId()).getId());
        List<Map<String,Object>> result = orderService.list(params);
        return result;
    }

    @RequestMapping("/cancelOrder")
    @ResponseBody
    public Map<String, Object> cancelOrder(@RequestParam Map<String, String> params) {
        Map<String, Object> result = orderService.cancelOrder(params);
        return result;
    }

    @RequestMapping("/confirmReceive")
    @ResponseBody
    public Map<String, Object> confirmReceive(@RequestParam Map<String, String> params) {
        Map<String, Object> result = orderService.confirmReceive(params);
        return result;
    }

    @RequestMapping("/deliverGood")
    @ResponseBody
    public Map<String, Object> deliverGood(Order order) {
        Map<String, Object> result = orderService.deliverGood(order);
        return result;
    }

    @RequestMapping("/orderDetail")
    public ModelAndView orderDetail(@RequestParam Map<String, String> params){
        ModelAndView modelAndView = new ModelAndView();
        Order order = orderService.selectByPrimaryKey(Integer.parseInt(params.get("id")));
        Receiver receiver = receiverService.selectByPrimaryKey(order.getReceiver());
        Sender sender = senderService.selectByPrimaryKey(order.getSender());
        if(params.get("deliver")!=null){
            Map<String,Object> map = new HashMap<String,Object>();
            List<Sender> senderList = senderService.list(map);
            modelAndView.addObject("senderList",senderList);
        }
        modelAndView.addObject("receiver",receiver);
        modelAndView.addObject("sender",sender);
        modelAndView.addObject("order",order);
        receiver.setProvince(icity.selectByCode(receiver.getProvince()).getName());
        receiver.setCity(icity.selectByCode(receiver.getCity()).getName());
        receiver.setArea(icity.selectByCode(receiver.getArea()).getName());
        modelAndView.setViewName("home/order/orderDetail");
        return modelAndView;
    }

    @RequestMapping("/goodList")
    @ResponseBody
    public List<Map<String,Object>> goodList(@RequestParam Map<String, Object> params){
        List<Map<String,Object>> result = orderService.goodList(params);
        return result;
    }

    @RequestMapping("/evaluatePage")
    public ModelAndView evaluatePage(@RequestParam Map<String, Object> params){
        ModelAndView modelAndView = new ModelAndView();
        Map<String,Object> good = orderService.selectGoodForEvaluate(params);
        Integer commentCount = goodService.getCommentCountByGoodId(good);
        Float averageScore = goodService.getAverageScoreByGoodId(good);
        if(averageScore==null)
            good.put("averageScore",5);//平均分
        else
            good.put("averageScore",averageScore);
        if(commentCount>0){
            Integer goodCommentCount = goodService.getGoodCommentCountByGoodId(good);
            Integer rateOfPraise= (int)((float)goodCommentCount/commentCount*100);
            good.put("rateOfPraise",rateOfPraise);//好评率
        }else{
            good.put("rateOfPraise",100);//好评率
        }
        good.put("commentCount",commentCount);//评论数
        modelAndView.addObject("good",good);
        modelAndView.setViewName("home/order/evaluate");
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping(value = "/uploadCommentPhoto")
    public Map<String, Object> upload(@RequestParam(value = "file") MultipartFile file, @RequestParam Map<String, String> params) {
        com.xungou.resource.pojo.Resource resource = FileUtil.uploadFile(file, "goodComment", iResourceService);
        Map<String, Object> map = new HashMap<>();
        map.put("photoId", resource.getId());
        return map;
    }
}